package com.cadre.common.utils.oldutil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/* loaded from: ExcelImpotUtils.class */
public class ExcelImpotUtils {
    private static final String xls = "xls";
    private static final String xlsx = "xlsx";
    private static FormulaEvaluator evaluator;

    public static List<Map<Integer, String>> readExcel(MultipartFile file) throws IOException {
        Workbook workbook = getWorkBook(file);
        List<Map<Integer, String>> list = new ArrayList<>();
        if (workbook != null) {
            evaluator = workbook.getCreationHelper().createFormulaEvaluator();
            for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
                Sheet sheet = workbook.getSheetAt(sheetNum);
                if (sheet != null) {
                    int firstRowNum = sheet.getFirstRowNum();
                    int lastRowNum = sheet.getLastRowNum();
                    for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
                        Map<Integer, String> map = new HashMap<>();
                        Row row = sheet.getRow(rowNum);
                        if (row != null) {
                            int firstCellNum = row.getFirstCellNum();
                            int lastCellNum = row.getPhysicalNumberOfCells();
                            String[] strArr = new String[row.getPhysicalNumberOfCells()];
                            for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
                                map.put(Integer.valueOf(cellNum), getCellValue(row.getCell(cellNum)));
                            }
                            list.add(map);
                        }
                    }
                }
            }
            workbook.close();
        }
        return list;
    }

    public static Workbook getWorkBook(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        Workbook workbook = null;
        try {
            InputStream is = file.getInputStream();
            if (fileName.endsWith(xls)) {
                workbook = new HSSFWorkbook(is);
            } else if (fileName.endsWith(xlsx)) {
                workbook = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
        }
        return workbook;
    }

    public static String getCellValue(Cell cell) {
        String cellValue;
        if (cell == null) {
            return "";
        }
        if (cell.getCellType().getCode()==0) {
            cell.setCellValue(1);
        }
        switch (cell.getCellType().getCode()) {
            case 0:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case 1:
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case DateUtil.SUB_MONTH:
                cellValue = getCellValue(evaluator.evaluateInCell(cell));
                break;
            case 3:
                cellValue = "";
                break;
            case 4:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case DateUtil.SUB_DAY:
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }
}
